// pages/allOrderList/allOrderList.js
import Notify from '../../components/dist/notify/notify';
import Toast from '../../components/dist/toast/toast';
var core = require("../../utils/core.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    page: 1, //请求第几页
    status: 0, //请求的订单状态 //  0：全部  1：处理中  2：已退款   3：已拒绝
    allData: [], //全部数据
    waitPayData: [], //待付款  处理中
    waitPeisongData: [], //待配送  已退款
    waitDeliveryData: [], //待提货  已拒绝
    dataStatus: 0, //数据状态  0不显示  1没有数据  2没有更多数据  
    pullDownRefresh: false, //是否是下拉刷新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      page: 1, //请求第几页  默认第一页
      status: 0, //请求的订单状态  默认0 
      dataStatus: 0
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
  },
  getList: function () {
    var that = this;
    that.setData({
      loading: true,
    })
    core.get("groupapp/refund/get_list", {
      page: that.data.page, //请求第几页  默认第一页
      status: that.data.status, //请求的订单状态  默认0 
    }, function (t) {

      // console.log(t);

      // return
      if (that.data.pullDownRefresh) {
        console.log('是下拉刷新');
        wx.stopPullDownRefresh()
        that.setData({
          pullDownRefresh: false
        })
      }
      if (that.data.status == 0) {//请求全部订单的情况
        if (that.data.page == 1) { //如果请求的是第一页
          if (t.list.length == 0) { //返回数据为空
            that.setData({
              loading: false,
              allData: [],
              dataStatus: 1
            })
          } else if (t.list.length < 10) {
            that.setData({
              loading: false,
              allData: t.list,
              dataStatus: 2
            })
          } else if (t.list.length == 10) {
            that.setData({
              loading: false,
              allData: t.list,
              dataStatus: 0
            })
          }
        } else if (that.data.page > 1) { //大于1页数据
          var arr = that.data.allData;
          arr = arr.concat(t.list);
          if (t.list.length == 0) { //返回数据为空
            console.log('等于0')
            that.setData({
              loading: false,
              allData: arr,
              dataStatus: 2
            })
          } else if (t.list.length < 10) { //大于1页数据  小于10条
            console.log('小于10')

            that.setData({
              loading: false,
              allData: arr,
              dataStatus: 2
            })
          } else if (t.list.length == 10) { //大于1页数据  等于10条
            console.log('等于10')
            that.setData({
              loading: false,
              allData: arr,
              dataStatus: 0
            })
          } else {
            console.log('else')
          }
        }
      } else if (that.data.status == 1) { //-------处理中的情况
        if (that.data.page == 1) { //如果请求的是第一页
          if (t.list.length == 0) { //返回数据为空
            that.setData({
              loading: false,
              waitPayData: [],
              dataStatus: 1
            })
          } else if (t.list.length < 10) {
            that.setData({
              loading: false,
              waitPayData: t.list,
              dataStatus: 2
            })
          } else if (t.list.length == 10) {
            that.setData({
              loading: false,
              waitPayData: t.list,
              dataStatus: 0
            })
          }
        } else if (that.data.page > 1) { //大于1页数据
          var arr = that.data.waitPayData;
          arr = arr.concat(t.list);
          if (t.list.length == 0) { //返回数据为空
            console.log('等于0')
            that.setData({
              loading: false,
              waitPayData: arr,
              dataStatus: 2
            })
          } else if (t.list.length < 10) { //大于1页数据  小于10条
            console.log('小于10')

            that.setData({
              loading: false,
              waitPayData: arr,
              dataStatus: 2
            })
          } else if (t.list.length == 10) { //大于1页数据  等于10条
            console.log('等于10')
            that.setData({
              loading: false,
              waitPayData: arr,
              dataStatus: 0
            })
          } else {
            console.log('else')
          }
        }
      } else if (that.data.status == 2) { //---------------------->已退款
        if (that.data.page == 1) { //如果请求的是第一页
          if (t.list.length == 0) { //返回数据为空
            that.setData({
              loading: false,
              waitPeisongData: [],
              dataStatus: 1
            })
          } else if (t.list.length < 10) {
            that.setData({
              loading: false,
              waitPeisongData: t.list,
              dataStatus: 2
            })
          } else if (t.list.length == 10) {
            that.setData({
              loading: false,
              waitPeisongData: t.list,
              dataStatus: 0
            })
          }
        } else if (that.data.page > 1) { //大于1页数据
          var arr = that.data.waitPeisongData;
          arr = arr.concat(t.list);
          if (t.list.length == 0) { //返回数据为空
            console.log('等于0')
            that.setData({
              loading: false,
              waitPeisongData: arr,
              dataStatus: 2
            })
          } else if (t.list.length < 10) { //大于1页数据  小于10条
            console.log('小于10')
            that.setData({
              loading: false,
              waitPeisongData: arr,
              dataStatus: 2
            })
          } else if (t.list.length == 10) { //大于1页数据  等于10条
            console.log('等于10')
            that.setData({
              loading: false,
              waitPeisongData: arr,
              dataStatus: 0
            })
          } else {
            console.log('else')
          }
        }
      } else if (that.data.status == 3) { //---------------》已拒绝
        if (that.data.page == 1) { //如果请求的是第一页
          if (t.list.length == 0) { //返回数据为空
            that.setData({
              loading: false,
              waitDeliveryData: [],
              dataStatus: 1
            })
          } else if (t.list.length < 10) {
            that.setData({
              loading: false,
              waitDeliveryData: t.list,
              dataStatus: 2
            })
          } else if (t.list.length == 10) {
            that.setData({
              loading: false,
              waitDeliveryData: t.list,
              dataStatus: 0
            })
          }
        } else if (that.data.page > 1) { //大于1页数据
          var arr = that.data.waitDeliveryData;
          arr = arr.concat(t.list);
          if (t.list.length == 0) { //返回数据为空
            console.log('等于0')
            that.setData({
              loading: false,
              waitDeliveryData: arr,
              dataStatus: 2
            })
          } else if (t.list.length < 10) { //大于1页数据  小于10条
            console.log('小于10')

            that.setData({
              loading: false,
              waitDeliveryData: arr,
              dataStatus: 2
            })
          } else if (t.list.length == 10) { //大于1页数据  等于10条
            console.log('等于10')
            that.setData({
              loading: false,
              waitDeliveryData: arr,
              dataStatus: 0
            })
          } else {
            console.log('else')
          }
        }
      } else if (that.data.status == 4) {
        if (that.data.page == 1) { //如果请求的是第一页
          if (t.list.length == 0) { //返回数据为空
            that.setData({
              loading: false,
              AlreadyDeliveryData: [],
              dataStatus: 1
            })
          } else if (t.list.length < 10) {
            that.setData({
              loading: false,
              AlreadyDeliveryData: t.list,
              dataStatus: 2
            })
          } else if (t.list.length == 10) {
            that.setData({
              loading: false,
              AlreadyDeliveryData: t.list,
              dataStatus: 0
            })
          }
        } else if (that.data.page > 1) { //大于1页数据
          var arr = that.data.AlreadyDeliveryData;
          arr = arr.concat(t.list);
          if (t.list.length == 0) { //返回数据为空
            console.log('等于0')
            that.setData({
              loading: false,
              AlreadyDeliveryData: arr,
              dataStatus: 2
            })
          } else if (t.list.length < 10) { //大于1页数据  小于10条
            console.log('小于10')

            that.setData({
              loading: false,
              AlreadyDeliveryData: arr,
              dataStatus: 2
            })
          } else if (t.list.length == 10) { //大于1页数据  等于10条
            console.log('等于10')
            that.setData({
              loading: false,
              AlreadyDeliveryData: arr,
              dataStatus: 0
            })
          } else {
            console.log('else')
          }
        }
      }
    })
  },
  changeTab: function (e) { //切换tab
    console.log(e)
    var that = this;
    var index = e.detail.index;
    this.setData({ //修改对应初始数据
      page: 1, //请求第几页  默认第一页
      status: index, //请求的订单状态  默认0 
      dataStatus: 0
    })
    this.clearArrData();
    that.getList();

  },
  clearArrData: function () {
    this.setData({
      allData: [], //全部数据
      waitPayData: [], //处理中
      waitPeisongData: [], //已退款
      waitDeliveryData: [], //已拒绝
    })
  },
  toEvaluate: function (e) {//去评价
    console.log(e);
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/orderEvaluate/orderEvaluate?id=' + id,
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    this.setData({ //修改对应初始数据
      page: 1, //请求第几页  默认第一页
      dataStatus: 0,
      pullDownRefresh: true
    })
    this.clearArrData();
    that.getList();
  },
  orderDetail: function (e) { //订单详情页面
    console.log(e)
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/AfterSaleRefundDetail/AfterSaleRefundDetail?id='+id,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var page = this.data.page;
    this.setData({
      page: page + 1
    })
    this.getList();
    console.log('上拉触底了');
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})